package com.lagou.edu.controller;

import com.lagou.edu.pojo.LoginParam;
import com.lagou.edu.pojo.Resume;
import com.lagou.edu.service.ResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
public class ResumeController {

    /**
     * Spring容器和SpringMVC容器是有层次的（父子容器）
     * Spring容器：service对象+dao对象
     * SpringMVC容器：controller对象，，，，可以引用到Spring容器中的对象
     */


    @Autowired
    private ResumeService resumeService;

    @GetMapping("resume")
    public ModelAndView queryAll(ModelAndView model) throws Exception {
        List list = resumeService.queryAccountList();
        model.addObject("resumeList", list);
        model.setViewName("list");
        return model;
    }


    @PostMapping("resume")
    public String add(Resume resume) {
        resumeService.save(resume);
        return "success";
    }

    @PostMapping("resume/update")
    public String update(Resume resume) {
        resumeService.save(resume);
        return "success";
    }

    @RequestMapping("resume/{id}")
    public String delete(@PathVariable("id") Long id) {
        resumeService.delete(id);
        return "success";
    }


    @RequestMapping("/login")
    public Object login(LoginParam loginParam,HttpSession httpSession) throws IOException {
        if("admin".equals(loginParam.getName())&&"admin".equals(loginParam.getPassword())){
            httpSession.setAttribute("token","admin");
            return "success";
        }
        return "auth fail";
    }


}
